import scala.Console.println

object SumDuration {
  def main(args: Array[String]): Unit = {

      //val lst1 = (1 to 10).toList
      //lst1.reduce(_+_)
      // 为什么这里能出现两个占位符？
      //lst1.reduce(_+_)
      // 我们说过一个占位符代表一个参数，那么两个占位符就代表两个参数。根据这个思路改写等价的语句
      // x类似于buffer，缓存每次操作的数据；y每次操作传递新的集合元素
      var b1 :User = new User("UserB","LocationB",10,60)
      var a1 :User = new User("UserA","LocationA",8,60)
      var a2 :User = new User("UserA","LocationA",9,60)
      var a3 :User = new User("UserA","LocationA",7,60)
      var a4 :User = new User("UserA","LocationA",12,60)

      var b2 :User = new User("UserB","LocationB",11,80)

      var b3:User = new User("UserB","LocationB",3,80)
      var map = scala.collection.mutable.Map[String, List[User]]()


      /*println("________"+lst2.get("a"))
      lst1.reduce((x, y) =>(x + y))
      // 利用reduce操作，查找 lst1 中的最大值
      lst1.reduce((x,y) => if (x>y) x else y)
      // reduceLeft、reduceRight
      lst1.reduceLeft((x,y) => if (x>y) x else y)
      lst1.reduceRight((x,y) => if (x>y) x else y)*/

      var users = List(b1,a1,a2,a3,a4,b2,b3)
      //println(users(0))
      //println(users.sorted)
      //println(users.+:(b3))
      //println(users.:+(b3))

      println(users)
      users = users.sorted
      println(users)

      users.drop(0)
    //println(users.dropRight(1))
      var i = 0
      while (i<users.length){
        var u:User = users(i)
        var ss :String= u.uId+u.lId
        var list = map.get(ss)
        if(list==None){
          var list2 = List(u)
          map.put(ss,list2)
        }else{
          var list2 =  list.get
          var j = list2.length-1
          var tempUser = list2(j)
          //u是本次被加
          if(u.startValue==tempUser.endValue){
            tempUser.endValue = (tempUser.endValue+u.length)
            list2 = list2.dropRight(1);
            list2 = (list2.:+(tempUser))
            map.put(ss,list2)
          }else{
            //map里面的list
            list2 = (list2.:+(u))
            map.put(ss,list2)
          }

        }
        i+=1
      }
    println("________________")
    for (elem <- map) {
      elem._2.foreach(el=>(el.setFinalLength()))
      elem._2.foreach(el=>(println(el.uId+"__"+el.lId+"__"+el.startDot+"__"+el.length)))
    }
    //map.map
    //println("________________")
     // println(map)



  }

 /* def addUser(map:scala.collection.mutable.Map):Unit={

  }*/
  case class User( uId:String,lId:String,startTime:Int,duration:Int) extends Ordered[User] {
    def compare(u:User ):Int = {
      if((uId+lId).compareTo(u.uId+u.lId)>=0){
        return startDot.compareTo(u.startDot)
      }else return -1

    }

    //用户id
    var userId = uId
    //位置id
    var locationId = lId
    //整点数
    var startDot = startTime
    //逗留时间段长
    var length = duration
    //整点数*60,以分钟为单位计算起点时间点
    var startValue = (startDot*60)
    //以分钟为单位计算结束时间点
    var endValue = (startValue+length)

    def setFinalLength():Unit={
      length = (endValue-startValue)
    }
  }
}

